﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace web_search
{
    public partial class FormUserSearch : Form
    {
        public FormUserSearch()
        {
            InitializeComponent();
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }


        private void LoadBook()
        {
            //首先连接数据库
            Daouser con = new Daouser();
            con.connect();

            //准备sql语句
            string sql = $"select BookID,ISBN,BookName,AuthorName,BookStatus,Price from V_UserBookInfor";
            
            
            //准备读取器
            SqlDataReader reader = con.read(sql);

            //while循环驱动读取器读取数据

            //添加前先清空表格
            dgv.Rows.Clear();

            //while循环驱动读取器读取数据
            while (reader.Read())
            {
                string bookstatus = "";
                if (reader[4].ToString() == "0")
                {
                    bookstatus = "在馆";
                }
                else
                {
                    bookstatus = "已借出";
                }

                //一行一行加进去
                dgv.Rows.Add(reader[0].ToString(), reader[1].ToString(), reader[2].ToString(),
                    reader[3].ToString(), bookstatus, reader[5].ToString()   
                    );

            }

            //关闭数据库
            reader.Close();
            
            con.DaoClose();
        }


        private void FormUserSearch_Load(object sender, EventArgs e)
        {
            //在加载的同时，将数据库的所有条目加载到表格里面

            LoadBook();
        }

        private void FormUserSearch_SizeChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void fleshbtn_Click(object sender, EventArgs e)
        {
            LoadBook();
            MessageBox.Show("刷新成功！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //如果点击的这一行本身就为空，或者填充的数据为空，把当前选中改成null
            if (dgv.CurrentRow == null || dgv.CurrentRow.Cells[1].Value == null)
            {
                SelectISBN.Text = "Null";
                SelectName.Text = "Null";
            }
            else //不为空，要把当前选中改成选中部分
            {
                string isbn = dgv.CurrentRow.Cells[1].Value.ToString();
                string name = dgv.CurrentRow.Cells[2].Value.ToString();
                SelectISBN.Text = isbn;
                SelectName.Text = name;
            }
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void label3_Click_1(object sender, EventArgs e)
        {

        }

        private void searchbtn_Click(object sender, EventArgs e)
        {
            
            //先产生关键字（去除前导空格和后面空格）
            string key_isbn = key1.Text.Trim();
            string key_name = key2.Text.Trim();
            string key_author = key3.Text.Trim();
            string key_bookid = key4.Text.Trim();

            //先判断是否为空，再设置条件condition
            string condition = " where 1=1 ";
            if (key_isbn != "")
            {
                condition += $"and ISBN like '%{key_isbn}%'";
            }
            if (key_name != "")
            {
                condition += $"and BookName like '%{key_name}%'";
            }
            if (key_author != "")
            {
                condition += $"and AuthorName like '%{key_author}%'";
            }
            if (key_bookid != "")
            {
                condition += $"and BookID like '%{key_bookid}%'";
            }

            //连接数据库
            Daouser con=new Daouser();
            con.connect();

            //准备sql语句
            string sql = $"select BookID,ISBN,BookName,AuthorName,BookStatus,Price from V_UserBookInfor" + condition;

            //准备读取器
            SqlDataReader reader = con.read(sql);
            //添加前先清空表格
            dgv.Rows.Clear();

            //while循环驱动读取器读取数据
            while (reader.Read())
            {
                string bookstatus = "";
                if (reader[4].ToString() == "0")
                {
                    bookstatus = "在馆";
                }
                else
                {
                    bookstatus = "已借出";
                }

                //一行一行加进去
                dgv.Rows.Add(reader[0].ToString(), reader[1].ToString(), reader[2].ToString(),
                    reader[3].ToString(), bookstatus, reader[5].ToString()
                    );
                //关闭数据库
            }
            reader.Close();
            con.DaoClose();
        }

        private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void key1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
